import os

import requests

# 接口 URL
url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload"

# access_token 和 media 类型
access_token = "uHjqBTIfBaFbalk9I-HgK4xhR2byH5SGA8oV2cRKZxFBRNptN1ZastPORbmTvmNfuUiJViye5BXqQok9MrCmAHF9r3RtJ876SQrwe-DCNzF0k_fzu_xCRN9DrYU2E8w_V5nqZiXB5NHCHG-JicvciwfxZZV3iAKFcTIAshD9Bk9zs5isUGa11clLL5YQBLBdxTXpfEatdbYtskn5WVVY3g"  # 替换为你的 access_token
media_type = "image"  # 或者其他媒体文件类型

# 要上传的文件
file_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "file", "upload.jpeg")
# 替换为你的文件路径
files = {'media': ('filename.jpg', open(file_path, 'rb'), 'application/octet-stream')}
print(files)
# 发送 POST 请求
response = requests.post(url, params={'access_token': access_token, 'type': media_type}, files=files)

# 关闭文件
files['media'][1].close()

# 检查响应
if response.status_code == 200:
    result = response.json()
    print("请求成功，响应数据：", result)
else:
    print("请求失败，状态码：", response.status_code)


print(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "file"))
print(type(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "file")))